package com.lemon.blog.system.controller;

import com.lemon.blog.system.domain.BlFrontMenu;
import com.lemon.blog.system.service.BlFrontMenuService;
import com.lemon.common.core.constant.UserConstants;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.log.annotation.Log;
import com.lemon.common.log.enums.BusinessType;
import com.lemon.common.security.annotation.RequiresPermissions;
import com.lemon.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 包名称：com.lemon.blog.controller
 * 类名称：BlCommentController
 * 类描述：前台菜单信息controller
 * 创建人：@author huangpeng
 * 创建时间：2022年09月25日 17:08
 */
@Api("前台菜单管理")
@RestController
@RequestMapping("/menu")
public class BlFrontMenuController extends BaseController {

    @Autowired
    private BlFrontMenuService menuService;

    /**
     * 获取菜单列表
     * @Title: list
     * @Description: 获取菜单列表
     * @param menu
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:43
     */
    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    public AjaxResult list(BlFrontMenu menu) {
        List<BlFrontMenu> menus = menuService.selectMenuList(menu);
        return AjaxResult.success(menus);
    }

    /**
     * 根据菜单id获取详细信息
     * @Title: getInfo
     * @Description: 根据菜单id获取详细信息
     * @param menuId
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @RequiresPermissions("system:menu:query")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(menuService.selectByPrimaryKey(menuId));
    }

    /**
     * 获取菜单下拉树列表
     * @Title: treeSelect
     * @Description: 获取菜单下拉树列表
     * @param menu
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(BlFrontMenu  menu) {
        List<BlFrontMenu > menus = menuService.selectMenuList(menu);
        return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 新增菜单
     * @Title: add
     * @Description: 新增菜单
     * @param menu
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @RequiresPermissions("system:menu:add")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BlFrontMenu  menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        menu.setCreateBy(SecurityUtils.getUsername());
        return toAjax(menuService.insert(menu));
    }

    /**
     * 修改菜单
     * @Title: edit
     * @Description: 修改菜单
     * @param menu
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @RequiresPermissions("system:menu:edit")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BlFrontMenu  menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        else if (menu.getId().equals(menu.getParentId()))
        {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menu.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(menuService.update(menu));
    }

    /**
     * 删除菜单
     * @Title: remove
     * @Description: 删除菜单
     * @param menuId
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @RequiresPermissions("system:menu:remove")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId))
        {
            return AjaxResult.error("存在子菜单,不允许删除");
        }
        return toAjax(menuService.deleteByPrimaryKey(menuId));
    }

    /**
     * 获取路由信息
     * @Title: getRouters
     * @Description: 获取路由信息
     * @param
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/2/26 18:44
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        List<BlFrontMenu > menus = menuService.selectMenuTree();
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}